<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>日志管理</el-breadcrumb-item>
      <el-breadcrumb-item>操作日志</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <div>
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item label="操作人">
            <el-input type="text" v-model="queryInfo.username" placeholder="请输入操作人"></el-input>
          </el-form-item>
          <el-form-item label="IP地址">
            <el-input v-model="queryInfo.ip" placeholder="请输入IP地址"></el-input>
          </el-form-item>
          <el-form-item label="IP地址">
            <el-input v-model="queryInfo.location" placeholder="请输入操作位置"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button  icon="el-icon-search" @click="getLoginLogList" type="primary">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button @click="deletelist(sels)" icon="el-icon-delete"  :disabled="this.sels.length === 0">批量</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table ref="multipleTable" :data="LoginlogList" style="width: 100%" @selection-change="selsChange" stripe>
        <el-table-column type="selection"  label="全选" width="55" ></el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="operation" label="操作" width="180"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="method" label="方法" width="160"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="params" label="参数" width="200"></el-table-column>
        <el-table-column prop="times" label="耗时" width="100">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.times>=2000" size="mini" type="danger" >
              {{scope.row.times+'毫秒'}}
            </el-tag>
            <el-tag size="mini" v-else-if="scope.row.times>=1000&&scope.row.times<=2000" >
              {{scope.row.times+'毫秒'}}
            </el-tag>
            <el-tag v-else  type="success" size="mini">
              {{scope.row.times+'毫秒'}}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="location" label="操作地点" width="200"></el-table-column>
        <el-table-column prop="ip" label="IP地址" width="160"></el-table-column>
        <el-table-column prop="username" label="操作人" width="130"></el-table-column>
        <el-table-column prop="ctime" label="时间" width="230"></el-table-column>
        <el-table-column label="操作" width="100">
          <template slot-scope="scope">
            <el-link type="primary" @click="deleteLog(scope.row.id)" class="el-icon-delete" size="small">删除</el-link>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.page"
      :page-sizes="[5, 10, 15, 30]"
      :page-size="queryInfo.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
export default {
  name: 'Log.vue',
  methods: {
    async getLoginLogList () {
      const { data: res } = await this.$http.get('sys/log', { params: this.queryInfo })
      if (res.code !== 200) {
        return this.$message.error(res.msg)
      }
      this.LoginlogList = res.data.data
      this.total = res.data.total
    },
    handleSizeChange (val) {
      this.queryInfo.size = val
      this.getLoginLogList()
    },
    handleCurrentChange (val) {
      this.queryInfo.page = val
      this.getLoginLogList()
    },
    deleteLog (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http.delete('sys/log/deletelog/' + id)
        if (res.code !== 200) {
          return this.$message.error(res.msg)
        }
        await this.getLogList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    deletelist () {
      var ids = this.sels.map(item => item.id).join()
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http.delete('sys/log/deleteList/' + ids)
        if (res.code !== 200) {
          return this.$message.error(res.msg)
        }
        await this.getLogList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    selsChange (sels) {
      this.sels = sels
    }
  },
  mounted () {
    this.getLoginLogList()
  },
  data () {
    return {
      sels: [],
      btnDisabled: false,
      btnLoading: false,
      LoginlogList: [],
      queryInfo: {
        username: '',
        ip: '',
        location: '',
        page: 1,
        size: 10
      }
    }
  }
}
</script>

<style scoped>

</style>
